import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from email.utils import formataddr


def send_email(content):
    from_addr = '1634983214@qq.com'  # 邮件发送账号
    to_addrs = ['17685034710@163.com', '1634983214@qq.com']  # 接收邮件账号，多个账号用列表

    # to_addrs = 'dengj@yelinked.com' 一个账号就这样
    authorization_code = 'sadbbheheqmjbiif'  # 授权码（这个要填自己获取到的）
    smtp_server = 'smtp.qq.com'  # 固定的SMTP服务器
    smtp_port = 465  # 固定端口

    # 配置服务器
    stmp = smtplib.SMTP_SSL(smtp_server, smtp_port)
    stmp.login(from_addr, authorization_code)

    # message = MIMEText('组装发送内容', 'plain', 'utf-8')   #发送的内容

    # 一个带附件的实例
    message = MIMEMultipart()
    message['From'] = formataddr(["王二麻子", from_addr])  # 发件人
    message['To'] = Header(header_name="自动化测试", charset='utf-8')  # 收件人
    subject = 'Python SMTP 邮件测试'
    message['Subject'] = Header(subject, 'utf-8')  # 邮件标题
    message.attach(MIMEText(content, 'plain', 'utf-8'))  # 邮件正文内容

    # 增加HTML附件
    atthtml = MIMEText(open('report.html', 'rb').read(), 'base64', 'utf-8')  # 文件放在同一路径，不放在同一路径改一下比如'D:/test/report.html
    atthtml["Content-Type"] = 'application/octet-stream'
    atthtml["Content-Disposition"] = 'attachment;filename = "report.html"'
    message.attach(atthtml)

    try:
        stmp.sendmail(from_addr, to_addrs, message.as_string())
        print('邮件发送成功')
    except Exception as e:
        print('邮件发送失败--' + str(e))
